/**
 * Notes: 消息模块业务逻辑 
 * Date: 2022-09-26 07:48:00 
 */

const BaseProjectService = require('./base_project_service.js');
const util = require('../../../framework/helpers/util.js');
const timeUtil = require('../../../framework/helpers/time_util.js');
const { miniLib } = require('../../../framework/lib_config.js');
const projectConfig = require('../public/project_config.js');

class MsgService extends BaseProjectService {

	getUserId(userId) {
		if (userId.includes('^^^'))
			return userId.split('^^^')[1];
		else
			return userId;
	}

	// 新审批
	async newFLow(userId, dept, person, cate, no, desc) {
		let title = '<<' + cate + '>>审批流程待审批';
		let content = person + '发起了<<' + cate + '>>的申请，流程编号:' + no + '，备注：' + desc; 
		
		// 发送给userId
		await this.insertMsg(title, content, 'flow', no, userId, 'admin');
	}

	// 审批结果
	async resultFlow(userId, person, cate, no, result, desc) {
		let title = '<<' + cate + '>>审批流程' + result;
		let content = person + '把你发起的<<' + cate + '>>申请' + result + '，流程编号:' + no + '，备注：' + desc; 

		// 发送给userId
		await this.insertMsg(title, content, 'flow', no, userId, 'user');
	}

	// 插入消息
	async insertMsg(title, content, type, param, userId, userType) {
		if (!title || !content || !userId) return;

		let data = {
			title,
			content,
			type,
			param,
			userId: this.getUserId(userId),
			userType: userType || 'user',
			status: 1,
			time: this._timestamp
		}

		try {
			const MsgModel = require('../config/models_config').msg_model;
			await MsgModel.insert(data);
		} catch (err) {
			console.error(err);
		}
	}

}

module.exports = MsgService;